﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace Cdc
{
    class WordInterop
    {
        public static void generateDoc(String TVA_date)
        {
            Application msWord = new Application();
            
            //msWord.ActivePrinter
            // mettez cette variable à true si vous souhaitez visualiser les opérations.
            msWord.Visible = true; 
            object missing = System.Reflection.Missing.Value;

            object fileName = @"Mon nouveau document.doc";
            object target = @"test.pdf";
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
            Microsoft.Office.Interop.Word.Document nvDoc;

            // Tester s'il s'agit d'un nouveau document ou d'un document existant.
            if (System.IO.File.Exists((string)fileName))
            {
                // ouvrir le document existant
                nvDoc = msWord.Documents.Open(ref fileName, ref missing, ref missing,
                            ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing,
                            ref missing);
            }
            else
            {
                // Choisir le template
                object templateName = System.IO.Directory.GetCurrentDirectory()+@"\Models\TVA.dot";
                // Créer le document
                nvDoc = msWord.Documents.Add(ref templateName, ref missing, ref missing, ref missing);
            }
            object field = "date_TVA";
            nvDoc.FormFields.get_Item(ref field).Result = TVA_date;

            nvDoc.SaveAs(ref target, ref format, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing);

            
        }

        public static void main(String[] arg)
        {
            WordInterop.generateDoc("Décembre 2010");
        }
    }
}
